# Author: Silveira Neto
# License: GPLv3
import sys, pygame
from pygame.locals import *
from pygame import Rect
from xml import sax

class Tileset:
	def __init__(self, file, tile_width, tile_height):
		image = pygame.image.load(file).convert_alpha()
		if not image:
			print "Error creating new Tileset: file %s not found" % file
		self.tile_width = tile_width
		self.tile_height = tile_height
		self.tiles = []
		for line in xrange(image.get_height()/self.tile_height):
			for column in xrange(image.get_width()/self.tile_width):
				pos = Rect(
						column*self.tile_width,
						line*self.tile_height,
						self.tile_width,
						self.tile_height )
				self.tiles.append(image.subsurface(pos))

	def get_tile(self, gid):
		return self.tiles[gid]

class TMXHandler(sax.ContentHandler):
	def __init__(self):
		self.width = 0
		self.height = 0
		self.tile_width = 0
		self.tile_height = 0
		self.columns = 0
		self.lines  = 0
		self.properties = {}
		self.tileset = None
		self.layer = 0
		self.CollisionArray = []

	def startElement(self, name, attrs):
		# get most general map informations and create a surface
		if name == 'map':
			self.columns = int(attrs.get('width', None))
			self.lines  = int(attrs.get('height', None))
			self.tile_width = int(attrs.get('tilewidth', None))
			self.tile_height = int(attrs.get('tileheight', None))
			self.width = self.columns * self.tile_width
			self.height = self.lines * self.tile_height
			self.image1 = pygame.Surface([self.width, self.height], SRCALPHA, 32)
			self.image2 = pygame.Surface([self.width, self.height], SRCALPHA, 32)
			for i in xrange(self.lines):
				self.CollisionArray.append([])
				for j in xrange(self.columns):
					self.CollisionArray[i].append(False)
		# create a tileset
		elif name=="image":
			source = attrs.get('source', None)
			self.tileset = Tileset(source, self.tile_width, self.tile_height)
		# store additional properties.
		elif name == 'property':
			self.properties[attrs.get('name', None)] = attrs.get('value', None)
		# starting counting
		elif name == 'layer':
			self.line = 0
			self.column = 0
			self.layer += 1
		# get information of each tile and put on the surface using the tileset
		elif name == 'tile':
			gid = int(attrs.get('gid', None)) - 1
			tile = self.tileset.get_tile(gid)
			pos = (self.column*self.tile_width, self.line*self.tile_height)
			if gid >= 0:
				if self.layer <=3:
					self.image1.blit(tile, pos)
				if self.layer >= 4:
					self.image2.blit(tile, pos)
			else:
				if self.layer == 2:
					self.CollisionArray[self.line][self.column] = True
			self.column += 1
			if(self.column>=self.columns):
				self.column = 0
				self.line += 1

	# just for debugging
	def endDocument(self):
		print "MapLoader"


def loadMap(file):
	parser = sax.make_parser()
	global tmxhandler
	tmxhandler = TMXHandler()
	parser.setContentHandler(tmxhandler)
	parser.parse(file)

def getCollisionArray():
		return tmxhandler.CollisionArray

def getImages():
		return tmxhandler.image1, tmxhandler.image2
	
def main():
	scrollx = 0
	scrolly = 0
	pygame.init()
	pygame.mixer.music.load("Lake_Hylia.mp3")
	pygame.mixer.music.play(-1)
	pygame.key.set_repeat(10, 10)
	screen = pygame.display.set_mode((800, 600))
	parser = sax.make_parser()
	tmxhandler = TMXHandler()
	parser.setContentHandler(tmxhandler)
	parser.parse("testmap.tmx")
	while 1:
		for event in pygame.event.get():
			if event.type == QUIT:
				return
			elif event.type == KEYDOWN and event.key == K_ESCAPE:
				return
			elif event.type == KEYDOWN:
				if event.key == K_LEFT:
					scrollx +=2
				if event.key == K_RIGHT:
					scrollx -=2
				if event.key == K_UP:
					scrolly +=2
				if event.key == K_DOWN:
					scrolly -=2
					
		screen.fill((255,255,255))
		screen.blit(tmxhandler.image1, (scrollx,scrolly))
		screen.blit(tmxhandler.image2, (scrollx,scrolly))
		pygame.display.flip()
		pygame.time.delay(1000/60)

if __name__ == "__main__": main()
